#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFileDialog>
#include<QMessageBox>
#include<QFile>
#include<QFontDialog>
#include<QColorDialog>
#include<QStyle>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->mainToolBar = addToolBar(tr("新建"));
    ui->mainToolBar->addAction(ui->actionnewFile);
    //ui->mainToolBar->addWidget(ui->actionnewFile);
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_actionnewFile_triggered()
{
    ui->statusBar->showMessage("You clicked newFile action.");
    ui->textEdit->setText("");
}
void MainWindow::on_pushButton_read_clicked()
{
    QString path = QFileDialog::getOpenFileName(this, "Open File...", "/Users/", "Text Files(*.txt);;Sourcecode File(*.cpp);;Header File(*.h)");
    if(path.length()==0)
    {
        QMessageBox::information(nullptr, "Open File Error", "你没有选中任何文件！");
        return;
    }
    //to do : read a file
    QFile file(path);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        //QByteArray str = file.readAll();
        while(!file.atEnd())
        {
            QByteArray str = file.readLine();
            ui->textEdit->setText(ui->textEdit->toPlainText()+str);
        }
        //ui->textEdit->setText(str);
    }
    file.close();
}
void MainWindow::on_pushButton_write_clicked()
{
    QString path =QFileDialog::getSaveFileName(this, "Open File...", "/Users/", "Text Files(*.txt);;Sourcecode File(*.cpp);;Header File(*.h)");
    if(path.length()==0)
    {
        QMessageBox::information(nullptr, "Open File Error", "你没有选中任何文件！");
        return;
    }
    //to do : write a file
    QFile file(path);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QString str = ui->textEdit->toPlainText();
        file.write(str.toUtf8());
    }
    file.close();
}
void MainWindow::on_actionopen_triggered()
{
    QString path = QFileDialog::getOpenFileName(this, "Open File...", "/Users/", "Text Files(*.txt);;Sourcecode File(*.cpp);;Header File(*.h)");
    if(path.length()==0)
    {
        QMessageBox::information(nullptr, "Open File Error", "你没有选中任何文件！");
        return;
    }
    ui->textEdit->setText("");
    //to do : read a file
    QFile file(path);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        //QByteArray str = file.readAll();
        while(!file.atEnd())
        {
            QByteArray str = file.readLine();
            ui->textEdit->setText(ui->textEdit->toPlainText()+str);
        }
        //ui->textEdit->setText(str);
    }
    file.close();
}
void MainWindow::on_actionsave_triggered()
{
    QString path =QFileDialog::getSaveFileName(this, "Open File...", "/Users/", "Text Files(*.txt);;Sourcecode File(*.cpp);;Header File(*.h)");
    if(path.length()==0)
    {
        QMessageBox::information(nullptr, "Open File Error", "你没有选中任何文件！");
        return;
    }
    //to do : write a file
    QFile file(path);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QString str = ui->textEdit->toPlainText();
        file.write(str.toUtf8());
    }
    file.close();
}
void MainWindow::on_actionfont_triggered()
{
    QFontDialog fontDlg(this);
    bool flag=true;
    QFont selectFont = fontDlg.getFont(&flag);
    ui->textEdit->setCurrentFont(selectFont);
}
void MainWindow::on_actioncolor_triggered()
{
    QColorDialog colorDlg(this);
    QColor color = colorDlg.getColor();
    //QStyle s;
    //s.
    //color: rgb(252, 2, 255);
    //ui->textEdit->setStyle(color);
}











